package com.terren.excavator.action;

import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.terren.excavator.config.ConfigInfo;
import com.terren.excavator.entity.LoginUser;
import com.terren.excavator.util.CommonUtil;
import com.terren.excavator.util.LoggerUtil;

/**
 * 登陆action
 * @author Administrator
 *
 */

@Controller("userLoginAction")
@Scope("prototype")
public class UserLoginAction extends ActionSupport{
	private static final long serialVersionUID = 1L;

	private LoginUser loginUser;//登陆的用户

	public LoginUser getLoginUser() {
		return loginUser;
	}
	public void setLoginUser(LoginUser loginUser) {
		this.loginUser = loginUser;
	}

	/**
	 * lucene 登陆
	 * @return
	 */
	public String luceneLogin(){
		LoginUser sessionUser = (LoginUser)CommonUtil.getSessionAttribute(ConfigInfo.SESSION_USER);
		
		if(sessionUser != null){//当前用户登陆状态时，直接返回
			return SUCCESS;
		}else{
			if(loginUser != null){
				if(ConfigInfo.LUCENE_USER_NAME.equals(loginUser.getUserName()) && ConfigInfo.LUCENE_PASS_WORD.equals(loginUser.getPassWord())){//登陆成功
					CommonUtil.setSessionAttribute(ConfigInfo.SESSION_USER, loginUser);
					return SUCCESS;
				}else{
					ActionContext.getContext().put("userName", loginUser.getUserName());
					ActionContext.getContext().put("loginErrorMsg", "用户名或密码错误！");
				}
			}else{
				ActionContext.getContext().put("loginErrorMsg", "");
			}
		}
		return LOGIN;
	}
	
	/**
	 * 注销
	 * @return
	 */
	public String logout(){
		HttpSession session = ServletActionContext.getRequest().getSession();
		session.removeAttribute(ConfigInfo.SESSION_USER);
		LoggerUtil.logInfo("logout....");
		return LOGIN;
	}
	
}
